//
//  KDInspectSecondController.m
//  WangJingApp
//
//  Created by 魏强 on 17/1/3.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDInspectSecondController.h"
#import "KDMListCellBaseModel.h"
#import "KDChildNewsController.h"

/**  test model */
#import "KDMonitorRequestTool.h"
#import "KDBaesMonitorModel.h"
#import "KDBaseParam.h"
#import "KDStatusResult.h"
#import "KDMGetWebListResult.h"
#import "KDMGetWeiboListResult.h"
#import "KDMGetAuthorListResult.h"
#import "KDMGetKeyWordListResult.h"
#import "KDMGetAppListResult.h"

#import "KDMBaseBoard.h"
#import "KDMBaseBoardModel.h"
@interface KDInspectSecondController ()<KDMBaseboardDelegate>
/**  <#Description#> */
@property (nonatomic, strong) NSArray *childArr;

/**
 *  所有数据的集合
 */
@property (nonatomic, strong) NSArray * themeArr;
/**
 *  地区类型
 */
@property (nonatomic, assign) NSInteger regionType;
/** navBar右边的自定义按钮*/
@property (nonatomic, weak) UIButton *rightBarBtn;
/**
 *  没有数据时的提示
 */
@property (nonatomic, weak) UIView * notice;
/** 右边弹出的选择界面*/
@property (nonatomic, weak) KDMBaseBoard *baseView;
/** 右边弹出的选择界面的遮盖*/
@property (nonatomic, weak) UIButton *cover;
@property (nonatomic, strong) NSDateFormatter *fmt;
/** 当前的baseBoardModel*/
@property (nonatomic, strong) KDMBaseBoardModel *baseBoardModel;
/** 默认的baseBoardModel*/
@property (nonatomic, strong) KDMBaseBoardModel * defaultBaseBoardModel;
@end

@implementation KDInspectSecondController
#pragma mark - 懒加载区
- (NSArray *)themeArr
{
    if (!_themeArr) {
        _themeArr = [NSArray array] ;
    }
    return _themeArr;
}
- (NSArray *)childArr
{
    if (!_childArr) {
        _childArr = @[@"OFweek激光网",@"新民网",@"青岛新闻网",@"暴恐",@"腾讯网",@"百度知道"] ;
    }
    return _childArr;
}
- (KDMBaseBoardModel *)baseBoardModel
{
    if (!_baseBoardModel) {//那里找不到了，就自己建
        _baseBoardModel = [[KDMBaseBoardModel alloc]init];
        _baseBoardModel.webSrcType = @"0";
        //        NSDate *newdate = [[NSDate date] initWithTimeInterval:- (24 *60 * 60 * 6 )sinceDate:[NSDate date]];
        _baseBoardModel.startTime = @"1970-01-01";//[self.fmt stringFromDate:newdate];
        _baseBoardModel.endTime = @"";
        _baseBoardModel.timeIndex = @"9";
        _baseBoardModel.docType = @"0";
        _baseBoardModel.bussiness = @"0";
        if (self.mainType == 3 || self.mainType == 1) {
            if (self.regionType == 0) {
                _baseBoardModel.regionType = 4;
            }else{
                _baseBoardModel.regionType = self.regionType;
            }
        }
        self.defaultBaseBoardModel = _baseBoardModel ;
    }
    return _baseBoardModel;
}
#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBar];
    [self loadData];
    
    
    
    
    // 添加所有子控制器
}
#pragma mark - 板块布置区
- (void)setupNavBar{
    [self setupleftBtn];
    [self setUpRightBarBtn];
}
/***  创建右边筛选按钮 */
- (void)setUpRightBarBtn
{
    UIButton *rightBarBtn = [[UIButton alloc]init];
    self.rightBarBtn = rightBarBtn;
    UIImage *image = [UIImage imageNamed:@"筛选"];
    rightBarBtn.width = image.size.width ;
    rightBarBtn.height = image.size.height;
    rightBarBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightBarBtn setImage:image forState:UIControlStateNormal];;
    [rightBarBtn addTarget:self action:@selector(rightBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBarBtn];;
    
}
- (void)setupAllChildViewController{
    for (KDBaesMonitorModel *model in self.themeArr) {
        KDChildNewsController *child = [[KDChildNewsController alloc]init];
        child.title = model.m_Name;
        child.model = model;
        child.view.backgroundColor = RandomColor;
        [self addChildViewController:child];
    }
    // 4.设置所有标题
    [super setupAllTitle];
}
- (void)setupleftBtn{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(backViewClick)];
}
/***  设置右边选择页面*/
-(void)setUpRightChoosePage
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (!self.cover) {
        //加蒙板
        UIButton *cover = [[UIButton alloc]initWithFrame:window.bounds];
        self.cover = cover;
        cover.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        
        [cover addTarget:self action:@selector(coverClick:) forControlEvents:UIControlEventTouchUpInside];
        [window addSubview:_cover];
    }
    self.cover.frame = window.bounds;
    //    KDLog/Users/MarkWei/Downloads/办理“深圳市重特大疾病补充医疗保险” 员工个人参保流程操作指引.doc(@"%@", NSStringFromCGRect(self.cover.frame));
    //创建底板
    if (!self.baseView) {
        KDMBaseBoard *baseView = [KDMBaseBoard baseboard];
        self.baseView = baseView;
        baseView.delegate = self;
        baseView.x = screenW;
        baseView.y = 64;
        baseView.height = screenH - baseView.y ;
        baseView.width = screenW - screenW*0.175;
        baseView.mainPageType = 3;
        
        [_cover addSubview:baseView];
    }
//    KDBaesMonitorModel *model;
//    if (self.themeArr.count) {
//        model = self.themeArr[self.currentIndex.integerValue];
//        _baseView.webBlockList = [self.webBlockDic objectForKey:model.m_Id];
//    }
    //    KDBaesMonitorModel *model = self.themeArr[self.currentIndex.integerValue];
    
    _baseView.showReginType = self.regionType;
    _baseView.height = screenH - _baseView.y ;
    _baseView.width = screenW - screenW*0.175;
    [UIView animateWithDuration:0.4 animations:^{
        _baseView.x = screenW*0.175;
    }];
}
#pragma mark - 事件点击区
- (void)backViewClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
/***  添加蒙板并显示右边页 */
- (void)rightBarBtnClick:(UIButton *)rightBarBtn
{
    if (!rightBarBtn.selected) {
        
        //设置右边的选择页面
        [self setUpRightChoosePage];
    } else
    {
        [self coverClick:self.cover];
    }
    rightBarBtn.selected = rightBarBtn.selected;
    
}
/***  去掉蒙板及右边页 */
- (void)coverClick:(UIButton *)cover
{
    [UIView animateWithDuration:0.4 animations:^{
        self.baseView.x = screenW;
        
    } completion:^(BOOL finished) {
        
        self.cover.frame = CGRectMake(0, 0, 0, 0);
        //        self.rightBarBtn.selected = NO;
        
    }];
    
}
#pragma mark - 网络请求区
- (void)loadData{
//    [self.notice removeFromSuperview];
    WS(ws);
    KDBaseParam *param = [[KDBaseParam alloc]init];
    switch (self.mainType) {
        case 0:
            param.method = @"getMonitorKeywordList";
            break;
        case 1:
            param.method = @"getMonitorWebSiteList";
            break;
        case 2:
            param.method = @"getMonitorAuthorList";
            break;
        case 3:
            param.method = @"getMonitorAppList";
            break;
        default:
            break;
    }
    [KDMonitorRequestTool getWithParam:param WithMainType:self.mainType success:^(KDStatusResult * responseObejct) {//正常情况下，就加载数据
        //        NSMutableDictionary *tempDic = [NSMutableDictionary new];
        if ([responseObejct.status isEqualToString:@"notlogin"]) {
            //写提示“该账号已在别处登录”
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"该账号已在别处登录" message:@"请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alertView show];
        }{
            //            KDMBodyViewModel *model = [[KDMBodyViewModel alloc]init];
            switch (ws.mainType) {
                case 0:{
                    KDMGetKeyWordListResult *result = (KDMGetKeyWordListResult *)responseObejct;
                    ws.themeArr =  result.info;
                    break;
                }
                case 1:{
                    KDMGetWebListResult *result = (KDMGetWebListResult *)responseObejct;
                    ws.themeArr =  result.info;
                    self.regionType = result.regionType;
                    break;
                }
                case 2:{
                    KDMGetAuthorListResult *result = (KDMGetAuthorListResult *)responseObejct;
                    ws.themeArr =  result.info;
                    break;
                }
                case 3:{
                    KDMGetAppListResult *result = (KDMGetAppListResult *)responseObejct;
                    ws.themeArr =  result.info;
                    self.regionType = result.regionType;
                    break;
                }
                default:
                    break;
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [ws setupAllChildViewController];
            });
            
        }
        
    } failure:^(NSError *error) {
        KDLog(@"%@",error);
//        [ws setUpNotise];
    }];
}



#pragma  mark - KDMBaseboardDelegate
/**
 *  确定按钮被点击了
 */
- (void)baseBoard:(KDMBaseBoard *)baseBoard
confirmBtnDidClickWithBaseBoardModel:(KDMBaseBoardModel *)model{
    self.baseBoardModel = model;
   KDChildNewsController *ctr = (KDChildNewsController *)self.childViewControllers[self.currentIndex];
    ctr.baseBoardModel = model;
    [ctr beginRefresh];
//    [self.currentTabV.header beginRefreshing];
    [self coverClick:self.cover];
}
#pragma mark - 通知接收区




@end
